package ai.people.netmon.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * url解析
 *
 * @author zhenglin
 * @since 2022/3/25
 */
public class UrlUtil {
    public static final Pattern COMPILE = Pattern.compile("(?i)(https?://)?(www\\.)?(.+)\\.com");

    /**
     * 从链接从获取域名
     *
     * www.baidu.com  -> baidu
     *
     * @param url url
     * @return {@link String}
     */
    public static String getHost(String url) {
        Matcher matcher = COMPILE.matcher(url);
        if (matcher.find()) {
            String group = matcher.group(3);
            if (group.contains(".")){
                String[] split = group.split("\\.");
                return split[split.length-1];
            }
            return group;
        }
        return null;
    }
}
